package cn.schoolwow.quickdao.module.database.ddl.query.flow.property.getEntityProperty;

import cn.schoolwow.quickdao.domain.entity.Entity;
import cn.schoolwow.quickdao.domain.entity.Property;
import cn.schoolwow.quickdao.module.database.parent.domain.ResultSetConsumer;
import cn.schoolwow.quickdao.module.database.parent.flow.executor.ExecuteQueryConnectionFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

import java.sql.ResultSet;
import java.util.Arrays;

public class GetEntityPropertyByOracleFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        Entity entity = (Entity) flowContext.checkData("entity");

        flowContext.startFlow(new ExecuteQueryConnectionFlow())
                .putTemporaryData("name", "获取指定表字段信息")
                .putTemporaryData("sql", "select column_name, data_type, nullable, data_length from user_tab_columns where table_name = ?")
                .putTemporaryData("parameters", Arrays.asList(entity.tableName))
                .putReturnData("resultSetConsumer",new ResultSetConsumer() {
                    @Override
                    public void consumeResultSet(ResultSet resultSet) throws Exception {
                        while(resultSet.next()){
                            Property property = new Property();
                            property.column = resultSet.getString("column_name");
                            property.columnType = resultSet.getString("data_type");
                            if(property.columnType.contains(" ")){
                                property.columnType = property.columnType.substring(0,property.columnType.indexOf(" "));
                            }
                            String dataLength = resultSet.getString("data_length");
                            if(property.columnType.toLowerCase().contains("char")&&null!=dataLength&&!dataLength.isEmpty()){
                                property.columnType += "(" + dataLength + ")";
                            }
                            property.notNull = "N".equals(resultSet.getString("nullable"));
                            entity.properties.add(property);
                        }
                    }
                })
                .execute();

        flowContext.startFlow(new ExecuteQueryConnectionFlow())
                .putTemporaryData("name", "获取指定表字段注释")
                .putTemporaryData("sql", "select column_name, comments from user_col_comments where table_name = ?")
                .putTemporaryData("parameters", Arrays.asList(entity.tableName))
                .putReturnData("resultSetConsumer",new ResultSetConsumer() {
                    @Override
                    public void consumeResultSet(ResultSet resultSet) throws Exception {
                        while(resultSet.next()){
                            String columnName =  resultSet.getString("column_name");
                            for (Property property : entity.properties) {
                                if (!property.column.equalsIgnoreCase(columnName)) {
                                    continue;
                                }
                                property.comment = resultSet.getString("comments");
                            }
                        }
                    }
                })
                .execute();
    }

    @Override
    public String name() {
        return "获取数据库表指定字段-Oracle数据库";
    }
}
